<template>
  <div>
    <div>
      {{isLogining ? '加载中' : '加载结束'}}
    </div>
    <weui-dialog :is_show="code_dialog_is_show" title="请输入验证码" :list="['提交']"
                 @on-click="on_code_dialog_click">
      <weui-input :list="code_dialog_input_list" title="" :show_more="false" :tips="null"
                  agreement_name="" more_label=""></weui-input>
    </weui-dialog>
  </div>
</template>

<script>
  import WeuiDialog from '../vue2componentSet/src/components/WeuiDialog'
  import WeuiInput from '../vue2componentSet/src/components/WeuiInput'
  import global from '../lib/global'
  import axios from 'axios'

  export default {
    components: {
      WeuiDialog,
      WeuiInput
    },
    data() {
      return {
        screenshot: '',
        isLogining: false,
        code_dialog_is_show: false,
        codeRequestId: 0,
        code: ''
      }
    },
    mounted() {
      this.$store.commit('set_title', '登录京东')

      if (!this.$store.state.admin) {
        this.$router.goback()
        return
      }

      this.isLogining = true
      axios.post(global.server + 'loginJd.do', {}).then(result => {
        this.isLogining = false
      }, result => {
        this.isLogining = false
      })
      this.get_code()
    },
    methods: {
      get_code() {
        if(!this.isLogining) {
          this.code_dialog_is_show = false
          return
        }
        axios.post(global.server + 'getFanliCode.do', {}).then(result => {
          const obj = result.data
          if (obj.code) {
            if (obj.codeRequestId != this.codeRequestId) {
              if (this.isLogining > 0) {
                this.code_dialog_is_show = true
                this.code = 'data:image/png;base64,' + obj.code
                this.codeRequestId = obj.codeRequestId
              }
            }
          } else {
            this.code_dialog_is_show = false
          }
          setTimeout(() => {
            this.get_code()
          }, 1000)
        }, result => {
          console.log(result)
          this.code_dialog_is_show = false
        })
      },
      on_code_dialog_click(index) {
        if (index == 0) {
          axios.post(global.server + 'submitFanliCode.do', {
            codeRequestId: this.codeRequestId,
            codeText: this.code_dialog_input_list[0].value
          }).then(result => {
          }, result => {
          })
        }
      },
    },
    computed: {
      code_dialog_input_list() {
        return [{
          value: '',
          img: this.code
        }]
      }
    }
  }
</script>
